﻿using System;

namespace Dotnet.Utils.Utility.ObjectExtensions.Number
{
    public static class ValueHelper
    {
        public static int? None2NullVal(int val)
        {
            int? result;
            if (val == -2147483648)
            {
                result = null;
            }
            else
            {
                result = new int?(val);
            }
            return result;
        }

        public static decimal? None2NullVal(decimal val)
        {
            decimal? result;
            if (val == -2147483648m)
            {
                result = null;
            }
            else
            {
                result = new decimal?(val);
            }
            return result;
        }

        public static DateTime? None2NullVal(DateTime val)
        {
            DateTime? result;
            if (val == NoneValue.NaDateTime)
            {
                result = null;
            }
            else
            {
                result = new DateTime?(val);
            }
            return result;
        }

        public static int NullVal2None(int? val)
        {
            int result;
            if (val != null && val != null)
            {
                result = val.Value;
            }
            else
            {
                result = int.MinValue;
            }
            return result;
        }

        public static decimal NullVal2None(decimal? val)
        {
            decimal result;
            if (val != null && val != null)
            {
                result = val.Value;
            }
            else
            {
                result = -2147483648m;
            }
            return result;
        }

        public static DateTime NullVal2None(DateTime? val)
        {
            DateTime result;
            if (val != null && val != null)
            {
                result = val.Value;
            }
            else
            {
                result = NoneValue.NaDateTime;
            }
            return result;
        }

        public static bool IsNone(int val)
        {
            return val == int.MinValue;
        }

        public static bool IsNone(int? val)
        {
            return val != null && val != null;
        }

        public static bool IsNone(decimal val)
        {
            return val == -2147483648m;
        }

        public static bool IsNone(decimal? val)
        {
            return val != null && val != null;
        }

        public static bool IsNone(DateTime val)
        {
            return val == NoneValue.NaDateTime;
        }

        public static bool IsNone(DateTime? val)
        {
            return val != null && val != null;
        }

        public static int None2Zero(int val)
        {
            int result;
            if (val == -2147483648)
            {
                result = 0;
            }
            else
            {
                result = val;
            }
            return result;
        }

        public static decimal None2Zero(decimal val)
        {
            decimal result;
            if (val == -2147483648m)
            {
                result = 0m;
            }
            else
            {
                result = val;
            }
            return result;
        }

        public static object None2Null(int val)
        {
            object result;
            if (val == -2147483648)
            {
                result = null;
            }
            else
            {
                result = val;
            }
            return result;
        }

        public static object None2Null(decimal val)
        {
            object result;
            if (val == -2147483648m)
            {
                result = null;
            }
            else
            {
                result = val;
            }
            return result;
        }

        public static object None2Null(DateTime val)
        {
            object result;
            if (val == NoneValue.NaDateTime)
            {
                result = null;
            }
            else
            {
                result = val;
            }
            return result;
        }

        public static object None2DBNull(int val)
        {
            object result;
            if (val == -2147483648)
            {
                result = DBNull.Value;
            }
            else
            {
                result = val;
            }
            return result;
        }

        public static object None2DBNull(decimal val)
        {
            object result;
            if (val == -2147483648m)
            {
                result = DBNull.Value;
            }
            else
            {
                result = val;
            }
            return result;
        }

        public static object None2DBNull(DateTime val)
        {
            object result;
            if (val == NoneValue.NaDateTime)
            {
                result = DBNull.Value;
            }
            else
            {
                result = val;
            }
            return result;
        }
    }
}